//
//  HomeViewController.m
//  FangXinLang
//
//  Created by AJY on 16/9/18.
//  Copyright © 2016年 AJY. All rights reserved.
//

#import "HomeViewController.h"

#import "BYConditionBar.h"
#import "SelectionButton.h"

#import "PushTableViewController.h"
#import "UIBarButtonItem+BarButtonItem.h"
#import "FTableViewController.h"
#import "SearchViewController.h"
#import "SGTopScrollMenu.h"
#import "FirstList.h"
#import "TitleModel.h"
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
@interface HomeViewController ()<SGTopScrollMenuDelegate,UITableViewDelegate>
//添加footscroll
@property(nonatomic,strong)UIScrollView *scroll;
@property(nonatomic,strong)FTableViewController *ftable;
//标题
@property(nonatomic,strong)NSMutableArray *titlearr;

@property(nonatomic,copy)NSString *url;
@property(nonatomic,strong)SGTopScrollMenu *topScrollMenu;
@property(nonatomic,strong)NSMutableArray *urlArr;
@property(nonatomic,strong)NSMutableArray *firstarr;
@property(nonatomic,strong)NSMutableArray *arrnameM;
@property(nonatomic,copy)NSString *st;
@property(nonatomic,strong)NSMutableArray *arrrr;
@end

@implementation HomeViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
//    self.automaticallyAdjustsScrollViewInsets=NO;
    
    //添加通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(getOperationsWithNoti:)
                                                 name:@"operations_from_selectionView"
                                               object:nil];

    [self additem];
    [self addChildViewController];
    //添加头标题
    [self settitleview];
    
    
}
//回调通知
-(void)getOperationsWithNoti:(NSNotification *)noti{
    [self.titlearr addObject:noti.userInfo[@"noti_title"]];
    //NSLog(@"%@",noti.userInfo[@"noti_title"]);
        self.topScrollMenu.titlesArr=self.titlearr;
    self.st=[self.titlearr lastObject];
    for(int i=0;i<self.arrrr.count;i++)
    {
        if([self.st isEqualToString:self.arrrr[i]])
        {
           
            [self.urlArr addObject:self.arrnameM[i]];
        }
    }

}

-(NSMutableArray *)titlearr{
    if(!_titlearr)
    {
        _firstarr=[NSMutableArray array];
        _titlearr=[NSMutableArray array];
        _urlArr=[NSMutableArray array];
        NSString *st=[[NSBundle mainBundle]pathForResource:@"First List.plist" ofType:nil];
        NSArray *arr=[NSArray arrayWithContentsOfFile:st];
        
        for (NSDictionary *dic in arr) {
            FirstList *model=[FirstList firstlistWith:dic];
            [self.firstarr addObject:model];
            [self.titlearr addObject:model.name];
            [self.urlArr addObject:model.url];
        }
            }
    return _titlearr;
}
-(NSMutableArray *)arrrr{
    if (_arrnameM == nil) {
        _arrnameM=[NSMutableArray array];
        _arrrr=[NSMutableArray array];
        NSArray *arr=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"addbtn.plist" ofType:nil]];
        for (NSDictionary *dic in arr) {

            [self.arrrr addObject:dic[@"name"]];
            [self.arrnameM addObject:dic[@"url"]];
        }
        
    }
    return _arrrr;
}

//设置标题
-(void)settitleview{
 
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.topScrollMenu=[SGTopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 60, ScreenW-40, 40)];
    self.topScrollMenu.indicatorViewColor=[UIColor redColor];
    self.topScrollMenu.titleColor=[UIColor blackColor];
    [self.topScrollMenu setSelectTitleColor:[UIColor redColor]];
    self.topScrollMenu.topScrollMenuDelegate = self;
    
    [self.view addSubview:self.topScrollMenu];
    
    [self addfootscroller];
    CGFloat x=CGRectGetMaxX(self.topScrollMenu.frame);
//跳转按钮
    SelectionButton *btn=[[SelectionButton alloc]initWithFrame:CGRectMake(x, 60, 40, 40)];
    [self.view addSubview:btn];
    //头标题的数组
    
    self.topScrollMenu.titlesArr=self.titlearr;
   
    
}
//创建自控制器

-(void)addChildViewController{
    for(int i=0;i<30;i++)
    {
        self.ftable=[[FTableViewController alloc]init];
        [self addChildViewController:self.ftable];
    }
}
-(UIScrollView *)scroll{
    if(!_scroll)
    {
        _scroll=[[UIScrollView alloc]init];
    }
    return _scroll;
}
-(void)addfootscroller{
//    self.scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0, ScreenW, ScreenH)];
    
    self.scroll.frame=CGRectMake(0,0, ScreenW, ScreenH);
    self.scroll.contentSize=CGSizeMake(ScreenW*self.titlearr.count, 0);
    //取消显示水平滚动
    self.scroll.showsHorizontalScrollIndicator=NO;
    //分页
    self.scroll.pagingEnabled=YES;
    //取消弹簧效果
    self.scroll.bounces=NO;
    //添加到titleview下边
    [self.view insertSubview:self.scroll belowSubview:self.topScrollMenu];
//显示控制器
    [self showViewController:0];
    }
-(void)SGTopScrollMenu:(SGTopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    CGFloat ofsetx=index * self.scroll.frame.size.width;
    //self.scroll.contentOffset=CGPointMake(ofsetx, 0);
    [self.scroll setContentOffset:CGPointMake(ofsetx, 0) animated:YES];
    //显示选定的控制器
    [self showViewController:index];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index=scrollView.contentOffset.x/scrollView.frame.size.width;
    [self showViewController:index];
    //[[NSNotificationCenter defaultCenter]postNotificationName:@"change" object:@(index)userInfo:nil];
    UILabel *la=self.topScrollMenu.allTitleLabel[index];
    [self.topScrollMenu selectLabel:la];
    [self.topScrollMenu setupTitleCenter:la];
}
-(void)showViewController:(NSInteger)indext{
    CGFloat ofsetx=indext *self.scroll.frame.size.width;
    FTableViewController *vc=self.childViewControllers[indext];
    //传网址
    vc.url=self.urlArr[indext];
    vc.name=self.titlearr[indext];
    vc.view.frame=CGRectMake(ofsetx, 0, ScreenW, ScreenH-40);
    // contentView的内边距
    CGFloat bottom = self.tabBarController.tabBar.frame.size.height;
    CGFloat top = CGRectGetMaxY(self.topScrollMenu.frame);
    // 代表导航栏和标题栏的总距离
    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    // 设置滚动条的内边距
    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    
    [self.scroll addSubview:vc.view];
   }
-(void)click{
    
}
-(void)additem{
    
    
    UIButton *b=[UIButton buttonWithType:UIButtonTypeCustom];
    UIBarButtonItem *btn=[UIBarButtonItem barButtonItemWith:b andTarget:nil andAction:nil andImage:@"navi_logo" andImageHeight:@"navi_logo"];
    self.navigationItem.leftBarButtonItem = btn;
    //右边推送按钮
    
    UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
    UIBarButtonItem *itemR1=[UIBarButtonItem barButtonItemWith:bt andTarget:self andAction:@selector(rightBtn1) andImage:@"navi_box_icon_hl@2x 2" andImageHeight:@"navi_box_icon_hl"];
    //右边🔍按钮
    
    UIButton *bt1=[UIButton buttonWithType:UIButtonTypeCustom];
    UIBarButtonItem *itemR2=[UIBarButtonItem barButtonItemWith:bt1 andTarget:self andAction:@selector(rightBtn2) andImage:@"navi_search_icon_hl@2x" andImageHeight:@"navi_search_icon_hl@3x"];
    self.navigationItem.rightBarButtonItems = @[itemR2,itemR1];
    
    
}
//今日推送的跳转
-(void)rightBtn1{
    PushTableViewController *push=[[PushTableViewController alloc]init];
    [self.navigationController pushViewController:push animated:YES];
    
}
-(void)rightBtn2{
    SearchViewController * login = [[SearchViewController alloc]init];
    login.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:login animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
